package com.ruoyi.cases.utils.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

/**
 * 导出
 */
public class ExportExcel2 {

    /**
     * * @param workbook
     *
     * @param title 标题
     * @param headers  列表头
     * @param dataset  数据
     * @param filepath  文件路径
     * @param istj   是否显示统计和
     * @throws IOException  抛出io异常
     */
    public void exportExcel(HSSFWorkbook workbook, String title, String[] headers,
        Collection dataset, String filepath, boolean istj)
        throws IOException {

        // 生成一个表格

        File file = new File(filepath);
        OutputStream out3 = null;
        List t = null;
        try {
            out3 = new FileOutputStream(filepath);
            // 生成一个表格
            HSSFSheet sheet = workbook.createSheet(title);
            // 设置表格默认列宽度为26个字节
            sheet.setDefaultColumnWidth((short) 26);
            // 生成一个样式 （标题行所有单元格样式）
            HSSFCellStyle style = workbook.createCellStyle();
            // 设置这些样式
            //  style.setFillForegroundColor(HSSFColor.WHITE.index);
            //  style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setAlignment(HorizontalAlignment.CENTER);
            // 生成标题单元格字体
            HSSFFont font = workbook.createFont();
            font.setFontName("宋体");//设置字体名称
            font.setFontHeightInPoints((short) 12);//设置字号
//            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);

            //数据行所有单元格样式
            HSSFCellStyle style2 = workbook.createCellStyle();
            //style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
            // style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//            style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//            style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//            style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
//            style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style2.setAlignment(HorizontalAlignment.LEFT);
            style2.setWrapText(true);//自动换行
            style2.setVerticalAlignment(VerticalAlignment.CENTER);
            // 生成数据单元格字体
            HSSFFont font2 = workbook.createFont();
            font2.setFontName("宋体");//设置字体名称
            font2.setFontHeightInPoints((short) 12);//设置字号
            style2.setFont(font2);

            // 产生表格标题行
            HSSFRow row = sheet.createRow(0);

            short j = 0;
            HSSFCell cell0 = row.createCell(j);
            cell0.setCellStyle(style);
            HSSFRichTextString text0 = new HSSFRichTextString("序号");
            cell0.setCellValue(text0);
            for (short i = 1; i <= headers.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellStyle(style);
                HSSFRichTextString text = new HSSFRichTextString(headers[i - 1]);
                cell.setCellValue(text);
            }
            List list = new ArrayList();
            // 遍历集合数据，产生数据行
            Iterator it = dataset.iterator();
            int index = 0;
            while (it.hasNext()) {
                index++;
                row = sheet.createRow(index);
                t = (List) it.next();

                short k = 0;
                HSSFCell cell1 = row.createCell(k);
                cell1.setCellStyle(style2);
                if (istj && !it.hasNext()) {
                    HSSFRichTextString text = new HSSFRichTextString("合计");
                    cell1.setCellValue(text);
                } else {
                    HSSFRichTextString text = new HSSFRichTextString(String.valueOf(index));
                    cell1.setCellValue(text);
                }

                // 利用反射，根据javabean属性的先后顺序，动态调用getXxx()方法得到属性值
                for (short i = 1; i <= t.size(); i++) {
                    HSSFCell cell = row.createCell(i);
                    cell.setCellStyle(style2);
                    // 判断值的类型后进行强制类型转换
                    String textValue = null;
                    if (t.get(i - 1) != null && !t.get(i - 1).toString().equals("")) {
                        textValue = t.get(i - 1).toString();
                    }
                    // 如果不是图片数据，就利用正则表达式判断textValue是否全部由数字组成
                    if (textValue != null) {
                        HSSFRichTextString richString = new HSSFRichTextString(textValue);
                        cell.setCellValue(richString);
                    }
                }
            }
            workbook.write(out3);
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            out3.close();

        }

    }

}
